require "rss/maker"

def rss(context, config)
  return true  unless config.key?(:filename)

  rss = RSS::Maker.make("2.0") do |maker|
    maker.channel.title = config[:title] || "Rire publish.rss"
    maker.channel.description = config[:description] || ""
    maker.channel.link = config[:link] || "http://www.example.com/"
    
    context.entries.each do |entry|
      item = maker.items.new_item

      item.title = entry.title || ''
      item.link = entry.link || "http://www.example.com/"
      item.description = entry.description || ''
      item.date = entry.date || Time.now

      if entry.key?(:enclosure)
        item.enclosure.url    = entry.enclosure.url || "http://www.example.com/"
        item.enclosure.type   = entry.enclosure.type || "text/plain"
        item.enclosure.length = entry.enclosure.length || 0
      end
    end
  end

  File::open(config[:filename], "w") {|w| w.puts rss }
end

